package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.*;
import com.cskaoyan.model.*;
import com.cskaoyan.model.System;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.HomeIndexVO;
import com.cskaoyan.model.vo.WxHomeCategoryVO;
import com.cskaoyan.service.WxHomeService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author ：王李轶群
 * @description：TODO
 * @date ：5/9/22 9:14 AM
 */
@Service
public class WxHomeServiceImpl implements WxHomeService {
    @Autowired
    AdMapper adMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GrouponMapper grouponMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    SystemMapper systemMapper;
    @Override
    public BaseRespVO<HomeIndexVO> homeIndex() {
        AdExample adExample = new AdExample();
        AdExample.Criteria adExampleCriteria = adExample.createCriteria();
        adExampleCriteria.andPositionEqualTo((byte)1);
        adExampleCriteria.andEnabledEqualTo(true);
        adExampleCriteria.andDeletedEqualTo(false);
        List<Ad> banner = adMapper.selectByExample(adExample);

        BrandExample brandExample = new BrandExample();

        BrandExample.Criteria brandCriteria = brandExample.createCriteria();
        brandCriteria.andDeletedEqualTo(false);
        SystemExample systemExample3 = new SystemExample();
        SystemExample.Criteria criteria3 = systemExample3.createCriteria();
        criteria3.andKeyNameEqualTo("market_wx_index_brand");
        List<System> brandValue = systemMapper.selectByExample(systemExample3);
        String brandV = brandValue.get(0).getKeyValue();
        PageHelper.startPage(1,Integer.parseInt(brandV));
        List<Brand> brandList = brandMapper.selectByExample(brandExample);

        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andLevelEqualTo("L1");
        categoryExampleCriteria.andDeletedEqualTo(false);
        List<Category> channel = categoryMapper.selectByExample(categoryExample);

        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria couponExampleCriteria = couponExample.createCriteria();
        couponExampleCriteria.andDeletedEqualTo(false);
        PageHelper.startPage(1,4);
        List<Coupon> couponList = couponMapper.selectByExample(couponExample);

        CategoryExample floorGoodsCategoryExample = new CategoryExample();
        CategoryExample.Criteria floorGoodsCategoryExampleCriteria = floorGoodsCategoryExample.createCriteria();
        floorGoodsCategoryExampleCriteria.andDeletedEqualTo(false);
        floorGoodsCategoryExampleCriteria.andLevelEqualTo("L1");
        SystemExample systemExample5 = new SystemExample();
        SystemExample.Criteria criteria5 = systemExample5.createCriteria();
        criteria5.andKeyNameEqualTo("market_wx_catlog_list");
        List<System> catlogValue = systemMapper.selectByExample(systemExample5);
        String catlogV = catlogValue.get(0).getKeyValue();
        PageHelper.startPage(1,Integer.parseInt(catlogV));
        List<Category> categories = categoryMapper.selectByExample(floorGoodsCategoryExample);
        SystemExample systemExample6 = new SystemExample();
        SystemExample.Criteria criteria6 = systemExample6.createCriteria();
        criteria6.andKeyNameEqualTo("market_wx_catlog_goods");
        List<System> goodsValue = systemMapper.selectByExample(systemExample6);
        String goodsV = goodsValue.get(0).getKeyValue();
        LinkedList<WxHomeCategoryVO> wxHomeCategoryVOS = new LinkedList<>();
        for (Category category : categories) {
            WxHomeCategoryVO wxHomeCategoryVO = new WxHomeCategoryVO();
            Integer id = category.getId();
            String name = category.getName();
            LinkedList<Goods> goods1 = new LinkedList<>();
            wxHomeCategoryVO.setId(id);
            wxHomeCategoryVO.setName(name);
            CategoryExample categoryExample1 = new CategoryExample();
            CategoryExample.Criteria criteria = categoryExample1.createCriteria();
            criteria.andPidEqualTo(id);
            criteria.andDeletedEqualTo(false);
            List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
            for (Category category1 : categories1) {
                Integer id1 = category1.getId();
                GoodsExample goodsExample = new GoodsExample();
                GoodsExample.Criteria criteria1 = goodsExample.createCriteria();
                criteria1.andCategoryIdEqualTo(id1);
                criteria1.andDeletedEqualTo(false);
                List<Goods> goods = goodsMapper.selectByExample(goodsExample);
                for (Goods good : goods) {
                    if(goods1.size()<Integer.parseInt(goodsV)){
                        goods1.add(good);
                    }else {
                        break;
                    }

                }
            }
            wxHomeCategoryVO.setGoodsList(goods1);
            wxHomeCategoryVOS.add(wxHomeCategoryVO);
        }

        GrouponExample grouponExample = new GrouponExample();
        GrouponExample.Criteria criteria = grouponExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Groupon> grouponList = grouponMapper.selectByExample(grouponExample);

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        goodsExampleCriteria.andDeletedEqualTo(false);
        goodsExampleCriteria.andIsHotEqualTo(true);
        SystemExample systemExample1 = new SystemExample();
        SystemExample.Criteria criteria1 = systemExample1.createCriteria();
        criteria1.andKeyNameEqualTo("market_wx_index_hot");
        List<System> hot = systemMapper.selectByExample(systemExample1);
        String hotValue = hot.get(0).getKeyValue();
        PageHelper.startPage(1,Integer.parseInt(hotValue));
        List<Goods> hotGoodsList = goodsMapper.selectByExample(goodsExample);

        GoodsExample newGoodsExample = new GoodsExample();
        GoodsExample.Criteria newGoodsExampleCriteria = newGoodsExample.createCriteria();
        newGoodsExampleCriteria.andDeletedEqualTo(false);
        newGoodsExampleCriteria.andIsNewEqualTo(true);
        SystemExample systemExample2 = new SystemExample();
        SystemExample.Criteria criteria2 = systemExample2.createCriteria();
        criteria2.andKeyNameEqualTo("market_wx_index_new");
        List<System> newValue = systemMapper.selectByExample(systemExample2);
        String newV = newValue.get(0).getKeyValue();
        PageHelper.startPage(1,Integer.parseInt(newV));
        List<Goods> newGoodsList = goodsMapper.selectByExample(newGoodsExample);

        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria topicExampleCriteria = topicExample.createCriteria();
        topicExampleCriteria.andDeletedEqualTo(false);
        SystemExample systemExample4 = new SystemExample();
        SystemExample.Criteria criteria4 = systemExample4.createCriteria();
        criteria4.andKeyNameEqualTo("market_wx_index_topic");
        List<System> topicValue = systemMapper.selectByExample(systemExample4);
        String topicV = topicValue.get(0).getKeyValue();
        PageHelper.startPage(1,Integer.parseInt(topicV));
        List<Topic> topicList = topicMapper.selectByExample(topicExample);

        HomeIndexVO homeIndexVO = new HomeIndexVO();
        homeIndexVO.setBanner(banner);
        homeIndexVO.setBrandList(brandList);
        homeIndexVO.setChannel(channel);
        homeIndexVO.setCouponList(couponList);
        homeIndexVO.setFloorGoodsList(wxHomeCategoryVOS);
        homeIndexVO.setGrouponList(grouponList);
        homeIndexVO.setHotGoodsList(hotGoodsList);
        homeIndexVO.setNewGoodslist(newGoodsList);
        homeIndexVO.setTopicList(topicList);
        BaseRespVO<HomeIndexVO> homeIndexVOBaseRespVO = new BaseRespVO<>();
        homeIndexVOBaseRespVO.setErrmsg("成功");
        homeIndexVOBaseRespVO.setErrno(0);
        homeIndexVOBaseRespVO.setData(homeIndexVO);
        return homeIndexVOBaseRespVO;
    }
}
